#include "KrwsParameter.h"

KrwsParameter::KrwsParameter(std::string fileName)
{
	clear();
	set(fileName);
}
KrwsParameter::KrwsParameter(void)
{
	clear();
}

KrwsParameter::~KrwsParameter(void)
{
}


void KrwsParameter::clear(void)
{
    seg_correct_dict.clear();
	lib_location.clear();
	delimiter_dict.clear();
	delimiter_dict.clear();
	word_list_dict.clear();
}

/*
KrwsParameter KrwsParameter::operator = (KrwsParameter * tmp) 
{
	lib_location  = tmp->libdataLocation;

	return * this;
}

KrwsParameter KrwsParameter::operator = (KrwsParameter tmp) 
{
	libdataLocation = tmp.libdataLocation;

	return * this;
}
*/
void KrwsParameter::print()
{
	std::cout << "parameter.lib_location:"		<<	lib_location	<<"<-"<<std::endl;
	std::cout << "parameter.dict_location:"		<<	dict_location	<<"<-"<<std::endl;
	std::cout << "parameter.delimiter_dict:"	<<	delimiter_dict	<<"<-"<<std::endl;
	std::cout << "parameter.word_list_dict:"	<<	word_list_dict	<<"<-"<<std::endl;
	std::cout << "parameter.seg_correct_dict:"	<<	seg_correct_dict	<<"<-"<<std::endl;

	std::cout << std::endl;
}

void KrwsParameter::set(std::string fileName)
{
	std::ifstream fin;

	fin.open(fileName.c_str());

	if( fin == NULL)
    {
		throw std::domain_error( fileName + "<-parameter file is not opend!! ");
    }

	try{
		read(fin);
		fin.close();
	}
	catch (std::exception &e)
	{
		throw e;
	}

}

void  KrwsParameter::read(std::istream &s) 
{
	//unsigned int i;
	std::string line, trimedLine;
	//st.setDelimiters(" \t=");
	std::vector<std::string> VParameters;


	
	while ( ! s.eof() )
	{
		std::getline(s , line);
//		std::cout << line << std::endl;

		line = StringTrim::trim( line );

		if( line.length() <= 0)
			continue;

		if( line.substr(0,1) =="#")
			continue;


		VParameters.clear();
		StringTokenizer::tokenize(line, VParameters, " \t=");

		if( VParameters.size() < 2 )
			continue;
		try{

			if( VParameters[0] == "lib_location" )
				lib_location = VParameters[1];

			if( VParameters[0] == "dict_location")
				dict_location = VParameters[1];

			if( VParameters[0] == "delimiter_dict")
				delimiter_dict = VParameters[1];

            if( VParameters[0] == "word_list_dict")
                word_list_dict = VParameters[1];

            if( VParameters[0] == "se_correct_dict")
                seg_correct_dict = VParameters[1];
//			else if( VParameters[0] == "addressDetectorFeatureRange" )
//				addressDetectorFeatureRange = atoi(VParameters[1].c_str());


		}
		catch(...)
		{
			throw std::domain_error("wrong input parameters form conf files");
		}
	}

}




